package com.ruoyi.develop.goodsSaledoGongxuUserMoney.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.constants.BusinessConstants;
import com.ruoyi.export.service.impl.ExportServiceImpl;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.develop.goodsSaledoGongxuUserMoney.domain.GoodsSaledoGongxuUserMoney;
import com.ruoyi.develop.goodsSaledoGongxuUserMoney.service.IGoodsSaledoGongxuUserMoneyService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 报工列Controller
 * @author qh
 * @date 2024-11-13
 */
@RestController
@RequestMapping("/goodsSaledoGongxuUserMoney/goodsSaledoGongxuUserMoney")
public class GoodsSaledoGongxuUserMoneyController extends BaseController {
    @Autowired
    private IGoodsSaledoGongxuUserMoneyService goodsSaledoGongxuUserMoneyService;
    @Autowired
    private ExportServiceImpl exportService;

    /**
     * 查询报工列列表
     */
    @PreAuthorize("@ss.hasPermi('develop:GoodsSaledoGongxuUserMoney:query')")
    @GetMapping("/list")
    public TableDataInfo list(GoodsSaledoGongxuUserMoney entity) {
        startPage();
        List<GoodsSaledoGongxuUserMoney> list = goodsSaledoGongxuUserMoneyService.selectList(entity);
        return getDataTable(list);
    }
    @GetMapping("/listAndParent")
    public TableDataInfo listAndParent(GoodsSaledoGongxuUserMoney entity) {
        startPage();
        List<GoodsSaledoGongxuUserMoney> list = goodsSaledoGongxuUserMoneyService.listAndParent(entity);
        return getDataTable(list);
    }
    @GetMapping("/listByGongxuId")
    public TableDataInfo listByGongxuId(GoodsSaledoGongxuUserMoney entity) {
        startPage();
        List<GoodsSaledoGongxuUserMoney> list = goodsSaledoGongxuUserMoneyService.listByGongxuId(entity);
        return getDataTable(list);
    }
    /**
     * 导出报工列列表
     */
    @PreAuthorize("@ss.hasPermi('develop:GoodsSaledoGongxuUserMoney:export')")
    @Log(title = "报工列", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GoodsSaledoGongxuUserMoney entity) {
        List<GoodsSaledoGongxuUserMoney> list = goodsSaledoGongxuUserMoneyService.selectList(entity);
        ExcelUtil<GoodsSaledoGongxuUserMoney> util = new ExcelUtil<GoodsSaledoGongxuUserMoney>(GoodsSaledoGongxuUserMoney. class);
        util.exportExcel(response, list, "报工列数据");
    }

    /**
     * 获取报工列详细信息
     */
    @PreAuthorize("@ss.hasPermi('develop:GoodsSaledoGongxuUserMoney:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(goodsSaledoGongxuUserMoneyService.selectById(id));
    }

    /**
     * 新增报工列
     */
    @PreAuthorize("@ss.hasPermi('develop:GoodsSaledoGongxuUserMoney:add')")
    @Log(title = "报工列", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody GoodsSaledoGongxuUserMoney entity) {
        return toAjax(goodsSaledoGongxuUserMoneyService.insert(entity));
    }

    @PostMapping("/addBaogong")
    public AjaxResult addBaogong(@RequestBody GoodsSaledoGongxuUserMoney entity) {
        int i=goodsSaledoGongxuUserMoneyService.insertBaogong(entity);
        if(i==0){
            return error("输入数量大于派工数量！");
        }else{
            return success("报工完成");
        }
    }

    @PostMapping("/addBaogongHebing")
    public AjaxResult addBaogongHebing(@RequestBody GoodsSaledoGongxuUserMoney entity) {
        return toAjax(goodsSaledoGongxuUserMoneyService.addBaogongHebing(entity));
    }
    /**
     * 修改报工列
     */
    @PreAuthorize("@ss.hasPermi('develop:GoodsSaledoGongxuUserMoney:edit')")
    @Log(title = "报工列", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody GoodsSaledoGongxuUserMoney entity) {
        return toAjax(goodsSaledoGongxuUserMoneyService.update(entity));
    }
    /**
     * 修改报工
     */
    @PutMapping("/editBaogong")
    public AjaxResult editBaogong(@RequestBody GoodsSaledoGongxuUserMoney entity) {
        return toAjax(goodsSaledoGongxuUserMoneyService.editBaogong(entity));
    }
    /**
     * 质检工序
     */
    @PutMapping("/editZhijian")
    public AjaxResult editZhijian(@RequestBody GoodsSaledoGongxuUserMoney entity) {
        return toAjax(goodsSaledoGongxuUserMoneyService.editZhijian(entity));
    }
    /**
     * 删除报工列
     */
    @PreAuthorize("@ss.hasPermi('develop:GoodsSaledoGongxuUserMoney:remove')")
    @Log(title = "报工列", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(goodsSaledoGongxuUserMoneyService.deleteByIds(ids));
    }

    @DeleteMapping("/removeUpdate/{ids}")
    public AjaxResult removeUpdate(@PathVariable String[] ids) {
        return toAjax(goodsSaledoGongxuUserMoneyService.deleteByIdsUpdate(ids[0]));
    }

    @DeleteMapping("/zhijianAllUpdate/{ids}")
    public AjaxResult zhijianAllUpdate(@PathVariable String[] ids) {
        return toAjax(goodsSaledoGongxuUserMoneyService.zhijianAllUpdate(ids));
    }

    @DeleteMapping("/zhijianAllNoUpdate/{ids}")
    public AjaxResult zhijianAllNoUpdate(@PathVariable String[] ids) {
        return toAjax(goodsSaledoGongxuUserMoneyService.zhijianAllNoUpdate(ids));
    }

    @Log(title = "工资信息导出", businessType = BusinessType.EXPORT)
    @GetMapping("/exportGz")
    public AjaxResult exportGz(GoodsSaledoGongxuUserMoney entity, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> params = new HashMap<>();
       // String date = DateUtils.getTime();//默认当天
        List<GoodsSaledoGongxuUserMoney> list = goodsSaledoGongxuUserMoneyService.listAndParent(entity);
        list.stream().forEach(e->{
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("doTime", DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, e.getDoTime()));
//            map.put("invoice", DictUtils.getDictLabel(BusinessConstants.YES_NO, e.getInvoice()));
//            map.put("fundCallback", DictUtils.getDictLabel(BusinessConstants.FUND_CALLBACK, e.getFundCallback()));
            e.setParams(map);
        });
        params.put("list", list);
        //params.put("date", date);
        String fileName = request.getParameter("fileName");
        if(StringUtils.isBlank(fileName)){
            fileName = "工资信息记录.xlsx";
        }
        String templateName = "工资信息记录.xlsx";
        exportService.baseXlsxExport(fileName, params, templateName, request, response);
        return null;
    }

    @Log(title = "报工信息导出", businessType = BusinessType.EXPORT)
    @GetMapping("/exportBogong")
    public AjaxResult exportBogong(GoodsSaledoGongxuUserMoney entity, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> params = new HashMap<>();
        // String date = DateUtils.getTime();//默认当天
        List<GoodsSaledoGongxuUserMoney> list = goodsSaledoGongxuUserMoneyService.listAndParent(entity);
        list.stream().forEach(e->{
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("doTime", DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, e.getDoTime()));
            map.put("statusName", DictUtils.getDictLabel("baogong_status", e.getStatus()));
            map.put("iszhijianName", DictUtils.getDictLabel("inspect_state", e.getIszhijian()));
//            map.put("invoice", DictUtils.getDictLabel(BusinessConstants.YES_NO, e.getInvoice()));
//            map.put("fundCallback", DictUtils.getDictLabel(BusinessConstants.FUND_CALLBACK, e.getFundCallback()));
            e.setParams(map);
        });
        params.put("list", list);
        //params.put("date", date);
        String fileName = request.getParameter("fileName");
        if(StringUtils.isBlank(fileName)){
            fileName = "报工信息记录.xlsx";
        }
        String templateName = "报工信息记录.xlsx";
        exportService.baseXlsxExport(fileName, params, templateName, request, response);
        return null;
    }
}
